﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using School.Entities;
namespace School.Pages.Evaluation
{

    public partial class edit_evaluation : System.Web.UI.Page
    {
        public static SchoolEntities db = new SchoolEntities();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                fillgridview1();
                Label1.Text = "";
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            fillgridview1();
        }

        private void fillgridview1()
        {
            Int64 x_grade = Int64.Parse(((DropDownList)evaluation1.FindControl("ddlGrade")).SelectedValue);
            Int64 x_subject = Int64.Parse(((DropDownList)(evaluation1.FindControl("ddlsubject"))).SelectedValue);
            Int64 x_class = Int64.Parse(((DropDownList)(evaluation1.FindControl("ddlclass"))).SelectedValue);
            int x_evaluation_type = int.Parse(((DropDownList)(evaluation1.FindControl("ddlevaluation_type"))).SelectedValue);

            var q1 = from r in db.TE_Evaluation
                     where (r.Grade_Id == x_grade  && r.Evaluation_Type_Id == x_evaluation_type)
                     select r;
            GridView1.DataSource = q1;
            GridView1.DataBind();
        }
        protected void gridview1_rowedting(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            fillgridview1();
        }
        protected void gridview1_rowcancelingedit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            fillgridview1();
        }
        protected void gridview1_rowdeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                fillgridview1();
                int x_id = int.Parse(((Label)GridView1.Rows[e.RowIndex].FindControl("l_id")).Text);
                var q1 = (from r in db.TE_Evaluation
                          where r.Id == x_id
                          select r).First();
                db.DeleteObject(q1);
                db.SaveChanges();
            }
            catch
            {
                Label1.Text = "لايمكنك حذف جميع البيانات ";
            }
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
          
                //if ((((TextBox)GridView1.Rows[e.RowIndex].FindControl("t_eva_name")).Text) == "" || (((TextBox)GridView1.Rows[e.RowIndex].FindControl("t_max_degree")).Text).ToString() == "" || (((TextBox)GridView1.Rows[e.RowIndex].FindControl("t_min_degree")).Text).ToString() == "")
                //{
                //    fillgridview1();
                //    Label1.Text = "تاكد من ادخال جميع البيانات";
                //}
                //else
                //{
                    string Name = (((TextBox)GridView1.Rows[e.RowIndex].FindControl("t_eva_name")).Text);
                    Int64 MaxDegree = Int64.Parse((((TextBox)GridView1.Rows[e.RowIndex].FindControl("t_max_degree")).Text));
                    Int64 MinDegree = Int64.Parse((((TextBox)GridView1.Rows[e.RowIndex].FindControl("t_min_degree")).Text));
                    int id = int.Parse(((TextBox)(GridView1.Rows[e.RowIndex].FindControl("t_id"))).Text);

                    var q1 = (from r in db.TE_Evaluation
                              where r.Id == id
                              select r).First();
                    q1.Name = Name;
                    q1.MaxDegree = MaxDegree;
                    q1.MinDegree = MinDegree;
                    db.SaveChanges();
                    GridView1.EditIndex = -1;
                    fillgridview1();
                    Label1.Text = "تم تعديل البيانات";
               
           // }

        }
    }
}